package SlidingWindow;

import java.util.ArrayList;
import java.util.List;

import static jdk.nashorn.internal.objects.NativeString.split;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-10-21
 * Time: 9:45
 */
class Solution7 {
    public List<Integer> findAnagrams(String ss, String pp) {
        List<Integer> list = new ArrayList<Integer>();
        char[] s = ss.toCharArray();
        char[] p = pp.toCharArray();

        int[] hashS = new int[26];
        int[] hashP = new int[26];

        for(char x : p){
            hashP[ x - 'a']++;
        }

        for(int right = 0 , left = 0 , count = 0 ; right < ss.length() ; right++ ){
            char in = s[right];
            hashS[ in - 'a']++;
            if(hashS[in - 'a'] <= hashP[in - 'a']){
                count++;//进入的是有效字符
            }
            char out = s[left];
            if(right - left + 1 > pp.length()){
                hashS[out - 'a']--;
                if(hashS[out - 'a'] < hashP[out - 'a']){
                    count--;
                }
                left++;
            }
            if(count == pp.length()){
                list.add(left);
            }
        }
        return list;

    }
}


public class Test7 {
    public static void main(String[] args) {
        String ss = "abab";
        String pp = "ab";
        Solution7 test = new Solution7();
        test.findAnagrams(ss,pp);
    }


}
